package com.frank.auth.application.context;

import com.google.common.collect.Maps;

import java.util.Map;
import java.util.Objects;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2024/3/23
 * @description 登录的上下文对象
 */
public class LoginContextHolder {

    private static final InheritableThreadLocal<Map<String, Object>> THREAD_LOCAL = new InheritableThreadLocal<>();

    public static Map<String, Object> getThreadLocalMap(){
        Map<String, Object> stringObjectMap = THREAD_LOCAL.get();
        if (Objects.isNull(stringObjectMap)){
            stringObjectMap = Maps.newConcurrentMap();
            THREAD_LOCAL.set(stringObjectMap);
        }
        return stringObjectMap;
    }

    public static void set(String key, Object value) {
        getThreadLocalMap().put(key, value);
    }

    public static Object get(String key) {
        return getThreadLocalMap().get(key);
    }

    public static String getLoginId(){
        return getThreadLocalMap().get("loginId").toString();
    }

    public static void remove() {
        THREAD_LOCAL.remove();
    }
}
